
from datetime import datetime
import sys
import tools

if __name__=="__main__":

    s = datetime.now()

    '''获取appid 和 appkey'''
    idkey = tools.getidkey()
    appid = idkey[0]
    appkey = idkey[1]

    files = tools.getFilesList('txt')
    files = [ item for item in files if not item.startswith('setting')]
    if len(files) <= 0:
        print('没有找到txt文件')
        input('按任意键退出...')
        sys.exit(0)

    fromLan, toLan = tools.getLanguage()

    for filepath in files:
        name = filepath.split('.')[0]
        res = []
        hanzi = []
        sumHanzi = 0
        with open(f'{name}.txt','r',encoding='utf-8') as file:
            lines = file.readlines()
            plen = len(lines)
            pcount = 1
            for line in lines:
                txt = tools.handle_text(line,fromLan)
                if tools.jump_text(txt):continue

                tran_text = tools.tran(txt, fromLan, toLan, appid, appkey)
                if toLan == 'zh': hanzi.append(tran_text + "\n");sumHanzi+=len(tran_text)
                print(tools.get_process(pcount, plen) + '---' + txt)
                res.append(txt)
                res.append(tran_text)
                pcount+=1

            with open(f'机翻-{name}.txt','w',encoding='utf-8') as resfile:
                resfile.writelines(res)
            #如果是俄语转汉语，需要统计汉语字数
            if toLan == 'zh':
                with open(f'汉字-{name}.txt', 'w', encoding='utf-8') as resfilehanzi:
                    resfilehanzi.writelines(hanzi)
                print(f'共计:{sumHanzi}个汉字')

        print(f"完成！共计:{tools.diff_time(s, datetime.now())}分钟")
    input('按任意键退出...')



